package com.leantech.android.browser.views

import android.content.Context
import android.content.res.Resources
import android.util.AttributeSet
import android.util.TypedValue
import androidx.collection.SimpleArrayMap
import com.leantech.android.browser.R
import com.leantech.android.browser.skin.SkinManager
import com.jaygoo.widget.RangeSeekBar
import com.qmuiteam.qmui.skin.IQMUISkinHandlerView
import com.qmuiteam.qmui.skin.QMUISkinManager
import com.qmuiteam.qmui.skin.defaultAttr.IQMUISkinDefaultAttrProvider
import com.qmuiteam.qmui.util.QMUIResHelper

class SkinRangeSeekBar @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null
) : RangeSeekBar(context, attrs), IQMUISkinDefaultAttrProvider,
    IQMUISkinHandlerView {

    companion object {
        private val DefaultAttrs = SimpleArrayMap<String, Int>(4)

        const val KeyProgressColor = "progressColor"
        const val KeyStepColor = "stepColor"
        const val KeyThumbDrawable = "thumbDrawable"
        const val KeyMarkTextColor = "markTextColor"

        init {
            DefaultAttrs.put(KeyProgressColor, R.attr.app_skin_range_seek_bar_progress_color)
            DefaultAttrs.put(KeyStepColor, R.attr.app_skin_range_seek_bar_step_color)
            DefaultAttrs.put(KeyThumbDrawable, R.attr.app_skin_range_seek_bar_thumb_drawable)
            DefaultAttrs.put(KeyMarkTextColor, R.attr.app_skin_range_seek_bar_mark_text_color)
        }
    }


    override fun handle(
        manager: QMUISkinManager,
        skinIndex: Int,
        theme: Resources.Theme,
        attrs: SimpleArrayMap<String, Int>?
    ) {
        if (attrs == null || attrs.isEmpty()) return
        for (i in 0 until attrs.size()) {
            val key = attrs.keyAt(i)
            val attr = attrs.valueAt(i)

            when (key) {
                KeyProgressColor -> {
                    val color = QMUIResHelper.getAttrColor(SkinManager.currentTheme(context), attr)
                    setProgressColor(color, color)
                }
                KeyStepColor -> {
                    val color = QMUIResHelper.getAttrColor(SkinManager.currentTheme(context), attr)
                    stepsColor = color
                }
                KeyThumbDrawable -> {
                    val currentTheme = SkinManager.currentTheme(context)
                    val ta = TypedValue()
                    currentTheme?.resolveAttribute(attr, ta, true)

                    if(ta.resourceId != 0){
                        leftSeekBar.thumbDrawableId = ta.resourceId
                    }
                }
                KeyMarkTextColor -> {
                    val color = QMUIResHelper.getAttrColor(SkinManager.currentTheme(context), attr)
                    tickMarkTextColor = color
                    tickMarkInRangeTextColor = color
                }

                else -> {
                    manager.defaultHandleSkinAttr(this, theme, key, attr)
                }
            }

        }

    }


    override fun getDefaultSkinAttrs(): SimpleArrayMap<String, Int>? {
        return DefaultAttrs
    }
}